package com.cs2340.practice;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class LocationDataBaseAdapter {

	
	// Columns
	public static final String KEY_NAME = "name";
	public static final String KEY_LONGITUDE = "logitude";
	public static final String KEY_LATITUDE = "latitude";
	public static final String KEY_ID = "_id";

	private static final String DATABASE_NAME = "LOCATION_DATABASE";
	private static final String DATABASE_TABLE = "LOCATION_TABLE";
	private static final int DATABASE_VERSION = 2;
	
	private String TAG = "location database";

	private DbHelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;
	
	
	/**
	 * An SQL helper class for the database.
	 */
	public static class DbHelper extends SQLiteOpenHelper {
		
		public DbHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
					KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
					KEY_NAME + " TEXT NOT NULL, " +
					KEY_LONGITUDE + " TEXT NOT NULL, " +
					KEY_LATITUDE + " TEXT NOT NULL);"
			);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}
	}

	/**
	 * Creates an adapter
	 * @param c - context for the adapter
	 */
	public LocationDataBaseAdapter(Context c) {
		ourContext = c;
	}
	

	/**
	 * Opens the database for writing.
	 * @return
	 */
	public LocationDataBaseAdapter openToWrite() throws android.database.SQLException {
		ourHelper = new DbHelper(ourContext);
		ourDatabase = ourHelper.getWritableDatabase();
		return this;
	}
	
	/**
	 * Closes the database.
	 */
	public void close() {
		ourHelper.close();
	}



	/**
	 * inserts a location into the database 
	 * 
	 * @param name location name 
	 * @param longitude locations longitude
	 * @param latitiude locations latitude
	 * @return
	 */
	public long insert(String name, String longitude, String latitude) {

		ContentValues contentValues = new ContentValues();
		contentValues.put(KEY_NAME, longitude);
		contentValues.put(KEY_LONGITUDE, longitude);
		contentValues.put(KEY_LATITUDE, latitude);
		
		Log.i(TAG, "Inserted: " + longitude + ", with longtitude " + longitude + " and latitude "+ latitude);
		
		return ourDatabase.insert(DATABASE_TABLE, null, contentValues);
	}

	/**
	 * deletes all users
	 * @return pointer to database
	 */
	public int deleteAll() {
		return ourDatabase.delete(DATABASE_TABLE, null, null);
	}
		

	/**
	 * Gets all the data in the database.
	 * @return A string of all the data in the database.
	 */
	public String getData() {

		String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_LONGITUDE, KEY_LATITUDE };

		Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null,
				null, null, null, null);

		String result = " ";

		int iRow = cursor.getColumnIndex(KEY_ID);
		int iName = cursor.getColumnIndex(KEY_NAME);
		int iLong = cursor.getColumnIndex(KEY_LONGITUDE);
		int iLat = cursor.getColumnIndex(KEY_LATITUDE);

		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

			result = result + cursor.getString(iRow) + " "
					+ cursor.getString(iName) + " " + cursor.getString(iLong)
					+ " " + cursor.getString(iLat)
					+ "\n";

		}

		return result;
	}
	
	/**
	 * Gets all of the locations in the database.
	 * @return An ArrayList of all of the locations in the database.
	 */
	public ArrayList<String> getLocations() {

		String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_LONGITUDE, KEY_LATITUDE };

		Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null,
				null, null, null, null);

		ArrayList<String> result = new ArrayList<String>();
		
		
		int iName = cursor.getColumnIndex(KEY_NAME);
		int x = 0;
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			Log.i(TAG, "Get locations for looped " + x + " times.");
			result.add(cursor.getString(iName));
			
			x++;
		}

		return result;
	}
	
	/**
	 * Goes into the database and grabs the longitude for the inputed location.
	 * @param username The location you want the longitude of.
	 * @return The longitude of the location.
	 */
	public String getLongitude(String location) {
		String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_LONGITUDE, KEY_LATITUDE };
		Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null,
				null, null, null, null);
		
		String longitude = "";
		
		int iLong = cursor.getColumnIndex(KEY_LONGITUDE);
		int iName = cursor.getColumnIndex(KEY_NAME);
		int x = 0;
		
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			Log.i(TAG, "Get usernames for looped " + x + " times.");
			if (cursor.getString(iName).equals(location)) {
				longitude = cursor.getString(iLong);
			} // end if
			x++;
		} // end for
		
		return longitude;
	}
	
	/**
	 * Goes into the database and grabs the latitude for the inputed location.
	 * @param location The location you want the latitude of
	 * @return The latitiude of the location.
	 */
	public String getLatitude(String location) {
		String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_LONGITUDE, KEY_LATITUDE };
		Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null,
				null, null, null, null);
		
		String latitude = "";
		
		int iLat = cursor.getColumnIndex(KEY_LATITUDE);
		int iName = cursor.getColumnIndex(KEY_NAME);
		int x = 0;
		
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			Log.i(TAG, "Get usernames for looped " + x + " times.");
			if (cursor.getString(iName).equals(location)) {
				latitude = cursor.getString(iLat);
			} // end if
			x++;
		} // end for
		
		return latitude;
	}

	/**
	 * removes a task from the data base
	 * @return if the task was succesfully deleted
	 */
	public int delete() {

		return 0;
	}

	/**
	 * queries for a certain task characteristic
	 * @param selection a category to query 
	 * @return pointer to query results
	 */
	public Cursor query(String selection) {

		String[] columns = new String[]{KEY_ID, KEY_NAME,KEY_LONGITUDE, KEY_LATITUDE};
		Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns,
		  selection, null, null, null, null);

		return cursor;

	}
	
	/*
	 * 
	 * updateContact()
    // Updating single contact
public int updateContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
 
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName());
    values.put(KEY_PH_NO, contact.getPhoneNumber());
 
    // updating row
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });
}

deleteContact()
    // Deleting single contact
public void deleteContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });
    db.close();
}
*/
}
